Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### Text In Sequential Files One common use for sequential files is to store text—such as a document or READ.ME file—that is not divided into fields. Each line, or record, in the file is simply treated as a line of text with no delimiters or other special characters. You can read and write text files using either traditional or FSO methods. Traditional Text File Access To write lines of text to a sequential file, use the Print # statement. Print # does not delimit fields with commas or enclose strings in double quotes. Rather, it writes data to the file with the exact same format as if the data had been displayed on the screen with the Print method. You saw Print # used in Chapter 11 in the Baby Editor project. The syntax for this statement is
    Print #filenum [, list] [,|;]
with these arguments: • filenum—The number associated with the file when it was opened (in OUTPUT or APPEND mode). • list—One or more string expressions to be written to the file. Multiple expressions in list should be separated by a comma or semicolon. If list is omitted, Print # writes a blank line to the file. The optional comma or semicolon at the end of the Print # statement determines the location of subsequent output to the file (that is, the output of the next Print # statement): • No comma or semicolon—Subsequent output is placed on a new line. • Semicolon—Subsequent output is placed on the same line immediately following the previous output. • Comma—Subsequent output is placed on the same line at the next print zone. Looking back to Chapter 9, you will see that the program outputs multiple lines to a file with a single Print # statement—a useful feature. If the text that is being output (in Chapter 11, for example, the Text property of a Text Box control) contains CR-LF characters, they serve to break the output into multiple lines in the sequential file. It has the same effect as writing each line of the text to the file with a separate Print # statement. You can see that Print # is quite flexible: It has the ability to output part of a line of text (if terminated with a semicolon or comma) or multiple lines of text (if the text contains its own CR-LF characters). If neither of these conditions is met, Print # outputs one entire line of text to the file. Let’s look at some other examples. The following illustrates what happens when a comma or semicolon appears at the end of the Print # statement: • Without a comma or semicolon Statements: Print #1, “Visual” Print #1, “Basic” Written to file: Visual Basic • With a semicolon Statements: Print #1, “Visual”; Print #1, “Basic” Written to file: VisualBasic • With a comma Statements: Print #1, “Visual”, Print #1, “Basic” Written to file: Visual Basic Next, let’s take a look at the difference between using a comma or a semicolon as a separator between expressions: • With a semicolon Statements: Print #1, “Visual”;“Basic” Written to file: VisualBasic • With a comma Statements: Print #1, “Visual”,“Basic” Written to file: Visual Basic To read lines of text one at a time from a sequential file, use the Line Input # statement. Line Input # reads an entire line of text from the file without regard to field delimiters, assigning it to a string variable. The syntax is
    Line Input [#]filenum, var
where the arguments are: • filenum—The number assigned to the file when it was opened (for INPUT). • var—The type String variable to receive the line of text. FSO Text File Access The FSO approach to file access is based upon something called a FileSystemObject. The first required step is to create an instance of this class:
    Dim fs
    Set fs = CreateObject(“Scripting.FileSystemObject”)
You can also use the following syntax:
    Dim fs As New Scripting.FileSystemObject
Note the required use of the Scripting qualifier, which identifies the library that the FileSystemObject class is defined in. You do not need to select this library, called the Microsoft Scripting Runtime, in the Visual Basic References dialog box to use the class, but doing so will give you access to the classes, methods, and properties in the Object Browser. The browser can be a useful source of information when you are programming. Remember, press F2, then select the desired library at the top left. Figure 13.1 shows information about the Scripting library displayed in the Object Browser. Once you have created an instance of the FileSystemObject class, the next step is to create a TextStream object, which is nothing more than a regular text file enclosed in an FSO wrapper. FileSystemObject has two methods for creating TextStream objects: • CreateTextFile—Creates a new text file. If a file of the same name already exists, it is overwritten. • OpenTextFile—Opens a text file for reading and/or writing. If the file already exists, new data is appended to existing data. Figure 13.1 Use the Object Browser to view library information. The syntax for these methods is similar. In these examples, assume that fs is a FileSystemObject and that ts has been declared as a type Variant or Object:
    Set ts = fs.CreateTextFile(filename[, overwrite[, unicode]])

    Set ts = fs.OpenTextFile(filename[, iomode[, create[, format]]])
The arguments for these statements are as follows: • filename—A string expression specifying the name, including path information, of the file. • overwrite—True or False indicating whether an existing file will be overwritten. If this argument is omitted, the default is False. If overwrite is False and filename already exists, an error occurs. You can trap this error to permit the user to verify file overwrites. • Unicode—True to create a Unicode file, False (the default) for an ASCII file. • iomode—Set to the constants ForReading or ForAppending to read the file or write data to the end of the file. You cannot write to a file opened in ForReading mode. • create—True or False specifying whether a new file will be created if filename does not exist. The default is False. • format—A tristate argument (one that can have three values) that determines the format of the file. TriStateTrue opens the file as Unicode, TriStateFalse (the default) opens the file as ASCII, and TriStateUseDefault uses the system default format setting. TIP: What Is Unicode? When dealing with text, computers use numbers to represent characters, punctuation marks, and so on. The two systems for this are ACSII (or ANSI) and Unicode. ASCII uses one byte per character, permitting only 256 different symbols to be represented. This is adequate for English, but cannot support many other languages. Unicode uses two bytes per character, permitting more than 65,000 different symbols. Which format you use depends on the specific requirements of your project.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.